package day05.text03;

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
import java.util.concurrent.ConcurrentHashMap;

public class MyIO1 {
    public static void main(String[] args) throws IOException {

        //读取userinfo的文件信息
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\Java\\group5InClass27\\src\\day05\\text03\\userinfo.txt")));

        ConcurrentHashMap<String,String>  cmp = new ConcurrentHashMap<>();

        //获取账号和密码
        String s;
        while ((s = br.readLine())!=null){
            String[] split = s.split(",");
            for (int i = 0; i < split.length; i++) {
                //将账号和密码传入集合中
                cmp.put(split[0],split[1]);
            }
        }
        br.close();

        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入账号");
            String idsc = sc.next();
           //判断cookie文件是否存在
            BufferedReader br2 = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\Java\\group5InClass27\\src\\day05\\text03\\cookie.txt")));
            String s1;
            while ((s1=br2.readLine())!=null){
                if(s1.equals(idsc)){
                    System.out.println("登入成功");
                    br2.close();
                    return;
                }
            }
            //判断userinfo文件中是否包含该账户
            if (cmp.containsKey(idsc)){
                System.out.println("请输入密码");
                String pswsc = sc.next();
                if(cmp.contains(pswsc)){
                    System.out.println("登入成功");
                    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\Java\\group5InClass27\\src\\day05\\text03\\cookie.txt",true)));
                    bw.write(idsc);
                    bw.newLine();
                    bw.flush();
                    bw.close();
                    break;
                }else {
                    System.out.println("你输入的密码错误");
                }
            } else {
                System.out.println("你输入的用户不存在");
            }
        }

    }
}
